﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WeAppsCenter.Core;
public static class Settings
{

    //TODO: 修改默认设置文件目录为可以更改
    public static string DataDirectory = "C:\\WeAppsCenter";
    private static readonly string SETTINGS_FILE_DIRECTORY = $"{DataDirectory}\\Settings.xml";
    public static string DefaultDirectory
    {
        get => GetDefaultDirectory();
        set => SetDefaultDirectory(value);
    }
    public static bool AutoUpdate
    {
        get => GetAutoUpdate();
        set => SetAutoUpdate(value);
    }
    static string GetDefaultDirectory()
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(SETTINGS_FILE_DIRECTORY);
        return xmlDocument.SelectSingleNode("/Settings/DefaultDirectory").InnerText;
    }
    static void SetDefaultDirectory(string v)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(SETTINGS_FILE_DIRECTORY);
        xmlDocument.SelectSingleNode("/Settings/DefaultDirectory").InnerText = v;
        xmlDocument.Save(SETTINGS_FILE_DIRECTORY);
    }
    static void SetAutoUpdate(bool v)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(SETTINGS_FILE_DIRECTORY);
        if (v)
        {
            xmlDocument.SelectSingleNode("/Settings/AutoUpdate").InnerText = "True";
        }
        else
        {
            xmlDocument.SelectSingleNode("/Settings/AutoUpdate").InnerText = "False";
        }
        xmlDocument.Save(SETTINGS_FILE_DIRECTORY);
    }
    static bool GetAutoUpdate()
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(SETTINGS_FILE_DIRECTORY);
        if (xmlDocument.SelectSingleNode("/Settings/AutoUpdate").InnerText == "True")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
